#!/bin/bash
# Copyright (C) 2025 Kumo inc.
# Author: Jeff.li lijippy@163.com
# All rights reserved.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https:#www.gnu.org/licenses/>.
#

# Smoke test to verify setup.py works as expected.
# Note on Windows, this must run via msys.

# Fail on any error. Treat unset variables an error. Print commands as executed.
set -eux

if [[ "$#" -ne "2" ]]; then
  echo 'Must specify the Python interpreter and virtualenv path.'
  echo 'Usage:'
  echo '  smoke_tests/smoke_test.sh [Python interpreter path] [virtualenv Path]'
  exit 1
fi

ABSL_PYTHON=$1
ABSL_VIRTUALENV=$2
TMP_DIR=$(mktemp -d)
trap "{ rm -rf ${TMP_DIR}; }" EXIT
# Do not bootstrap pip/setuptools, they are manually installed with get-pip.py
# inside the virtualenv.
if ${ABSL_VIRTUALENV} --help | grep '\--no-site-packages'; then
  no_site_packages_flag="--no-site-packages"
else
  # --no-site-packages becomes the default in version 20 and is no longer a
  # flag.
  no_site_packages_flag=""
fi
${ABSL_VIRTUALENV} ${no_site_packages_flag} --no-pip --no-setuptools --no-wheel \
    -p ${ABSL_PYTHON} ${TMP_DIR}

# Temporarily disable unbound variable errors to activate virtualenv.
set +u
if [[ $(uname -s) == MSYS* ]]; then
  source ${TMP_DIR}/Scripts/activate
else
  source ${TMP_DIR}/bin/activate
fi
set -u

trap 'deactivate' EXIT

# When running macOS <= 10.12, pip 9.0.3 is required to connect to PyPI.
# So we need to manually use the latest pip to install absl-py. See:
# https://mail.python.org/pipermail/distutils-sig/2018-April/032114.html
curl https://bootstrap.pypa.io/get-pip.py | python
pip --version

python --version
python setup.py install
python smoke_tests/sample_app.py --echo smoke 2>&1 |grep 'echo is smoke.'
python smoke_tests/sample_test.py 2>&1 | grep 'msg_for_test'
